Namespace Argus.Utilities

    Public Class INI
        '*********************************************************************************************************************
        '
        '             Class:  INI
        '      Initial Date:  12/03/2003
        '      Last Updated:  09/21/2007
        '     Programmer(s):  Blake Pell
        '       Description:  Windows style INI handling
        '      Dependencies:  N/A
        '  Additional Notes:  N/A
        '
        '*********************************************************************************************************************

        '****************************************************************************
        '  Variable Declarations
        '****************************************************************************
        ' Field length is the maximum field size, increase this constant to allow for larger fields
        Private Const FIELD_LENGTH = 1024

        '****************************************************************************
        '  Windows API Declarations
        '****************************************************************************
        Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
        Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer

        ''' <summary>
        ''' Constructor.  
        ''' </summary>
        ''' <param name="INIFileLocation">The location of the INI file that you want to read/write from.</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal INIFileLocation As String)
            _iniFile = INIFileLocation
        End Sub

        ''' <summary>
        ''' Function to read from a specified INI file
        ''' </summary>
        ''' <param name="mySection"></param>
        ''' <param name="myEntry"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetFromINI(ByVal mySection As String, ByVal myEntry As String) As String
            Dim Buf As String = ""
            Dim MyLength As Long = 0

            Buf = Space$(FIELD_LENGTH)

            MyLength = GetPrivateProfileString(mySection, myEntry, "", Buf, FIELD_LENGTH, _iniFile)

            GetFromINI = Left$(Buf, MyLength)
        End Function

        ''' <summary>
        ''' Function to write to a specified INI file
        ''' </summary>
        ''' <param name="Section"></param>
        ''' <param name="Entry"></param>
        ''' <param name="Buf"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function WriteToINI(ByVal Section As String, ByVal Entry As String, ByVal Buf As String) As Boolean
            Dim ReturnValue As Integer = 0

            ReturnValue = WritePrivateProfileString(Section, Entry, Buf, _iniFile)

            If ReturnValue = vbTrue Then
                WriteToINI = True
            Else
                WriteToINI = False
            End If
        End Function

        Private _iniFile As String = ""
        ''' <summary>
        ''' The location of the INI file.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property INIFileLocation() As String
            Get
                Return _iniFile
            End Get
            Set(ByVal value As String)
                _iniFile = value
            End Set
        End Property

    End Class
End Namespace